<?php
include_once("/var/www/html/new/com.inc.php");
include_once '/var/www/html/new/d/common/func.inc.php';

$ac1="123950";//短信群发系统的登录账号
$pw1 = "0b44dac903556e16a72a63992ba0b97e";
$s1=new SoapClient("http://open.12301.cc/openService/MXSE.wsdl",
                        array('encoding' =>'UTF-8','cache_wsdl' => 0));//票付通接口地址

$dtime = time();
$mobile=$_REQUEST['mobile'];
$pwd=substr($mobile,-6);
// $_REQUEST['action']='sendCode';
if($_REQUEST['action']=='sendCode'){ // 用户点击了发送验证码时执行
//    $_SESSION['valiCode'] = 111111;
//    $msg = '{"code":100,"msg":"发送验证码成功！"}';
//    exit($msg);
    if($mobile=="") exit('{"code":103,"msg":"请输入手机号"}');
    if(!ismobile($mobile)) exit('{"code":104,"msg":"手机格式不正确"}');
    $sql="select * from pft_member where mobile='$mobile' limit 1";
    $GLOBALS['le']->query($sql);
    $list=$GLOBALS['le']->fetch_assoc();
    if($list['account']) exit('{"code":102,"msg":"该手机号已存在"}');
    $time=time();
    $valiCode = getValiCode();
    if($_SESSION['lasttime']!=0){
        if(abs($time-$_SESSION['lasttime'])<60&&$_SESSION['mobile']==$mobile){
            exit('{"code":101,"msg":"您操作太频繁，请稍后再试"}');
        }else{
            $_SESSION['lasttime']=$time;
            $_SESSION['mobile']=$mobile;
        }
    }
    $_SESSION['lasttime']=$time;
    $_SESSION['valiCode'] = $valiCode;
    //echo $_SESSION['valiCode'];
    $sendphoneNo =''; //发送号码 为空，系统随机通过该注册用户绑定用户号码池中号码发送短信。 
    $recvPhoneNo ="$mobile"; //接收号码
    $msgContent ='欢迎使用票付通平台。验证码：'.$valiCode.'，输入验证继续完成操作。'; //接收内容
    // 发送至手机，成功返回 100
    $array = array(
        "ac"=>$ac1,
        "pw"=>$pw1,
        "in0"=>'fzpft',
        "in1"=>'ab8888',
        "in2"=>$mobile,
        "in3"=>$msgContent
    );
    $xml = $s1->__soapCall("SMS_User_Defined",$array);
    $obj = simplexml_load_string($xml);
    // $msg = ($xml->resultId == 1) ? '{"code":100,"msg":"发送验证码成功！"}':'{"code":101,"msg":"发送失败！"}';
    $msg = ((string)$obj->Rec->UUstatus==100) ? 
    '{"code":100,"msg":"发送验证码成功！"}':'{"code":101,"msg":"发送失败！"}';
    exit($msg);
}elseif($_REQUEST['action']=='register'){//注册
    include_once '/var/www/html/new/d/class/MemberAccount.class.php';
    $mem = new pft\Member\MemberAccount($GLOBALS['le']);
    $res=$mem->chkPassport($_REQUEST['mobile']);// 存在返回手机号
    $yanzhengma=$_REQUEST['valiCode'];
    if(!isset($_SESSION['valiCode'])){
        exit('{"code":102,"msg":"请先获取验证码，然后输入验证码校验手机号"}');
    }
    if($yanzhengma!=$_SESSION['valiCode']) exit('{"code":103,"msg":"验证码不正确,请重新输入"}');
    // if(!ismobile($mobile)) exit('{"code":101,"msg":"手机格式不正确"}');
    //if($res) exit('{"code":101,"msg":"手机号已存在"}');
    

    $dataMain = array(
        'mobile' => $mobile,
        'dtype'  => 5,
        'password'  => md5(md5($pwd)),
        'dname'=>$mobile,
    );
    // 邀请人
    // $domain = substr($_SERVER['HTTP_HOST'],0,strpos($_SERVER['HTTP_HOST'],'.'));
	$host = str_replace('www.', '', $_SERVER['HTTP_HOST']);
	$domain   = substr($host, 0, strpos($host, '.12301'));
    $sql="SELECT fid FROM `pft_member_domain_info` WHERE M_domain='$domain' or M_account_domain='$domain' LIMIT 1";
    $GLOBALS['le']->query($sql);
    $GLOBALS['le']->fetch_assoc();
    $applyDid = $GLOBALS['le']->f('fid');
    /*
    *如果店主是分销联盟的盟主  
    *那么根据当前正在注册的用户的IP地址
    *查下这个地区是否有市/省代理 
    *有的话设成邀请人 
    *都没有的话 再把店主设成邀请人
    */
    $sel_alliance = "select count(*) from pft_union_member_info_SE where fid=$applyDid limit 1";
    $GLOBALS['le']->query($sel_alliance);
    $GLOBALS['le']->fetch_assoc();
    $c_al = $GLOBALS['le']->f('count(*)');
    if($c_al!='1'){
        $dataMain['inviterID'] = $applyDid;
    }else{
        $city = get_local_address('city');//func.inc.php里的函数 获取正在注册的用户所在地
        $sel_code = "select n_code,parent_province from pft_area_code_concat where city like '%$city%' limit 1";
        $GLOBALS['le']->query($sel_code);
        $GLOBALS['le']->fetch_assoc();
        $n_code = $GLOBALS['le']->f('n_code');
        $parent_province = $GLOBALS['le']->f('parent_province');
        $sel_agency_c = "select inviterID from pft_distributor_agency where fid=$applyDid and darea_code=$n_code limit 1";
        $GLOBALS['le']->query($sel_agency_c);
        $row = $GLOBALS['le']->fetch_assoc();
        if($row['inviterID']){
            $dataMain['inviterID'] = $row['inviterID'];
        }else{
            if($n_code != $parent_province){
                $sel_agency_p = "select inviterID from pft_distributor_agency where fid=$applyDid and darea_code=$parent_province limit 1";
                $GLOBALS['le']->query($sel_agency_p);
                $row = $GLOBALS['le']->fetch_assoc();
                 if($row['inviterID']){
                    $dataMain['inviterID'] = $row['inviterID'];
                }else{
                    $dataMain['inviterID'] = $applyDid;
                }
            }else{
                $dataMain['inviterID'] = $applyDid;
            }
        }
    }
//     print_r($dataMain);
//     exit;
    $result = $mem->register($dataMain);
    if($result['status']!='ok') {
        exit('{"code":101,"msg":"用户注册失败。失败原因：'.$result['msg'].'"}');
    }else{
        // 写入 $_SESSION
        $body = explode('|',$result['body']);
        $_SESSION['memberID'] = $body[0];
        $_SESSION['sid'] = $body[0];
        $_SESSION['dname'] = $mobile;
        $_SESSION['account'] = $mobile;
        $_SESSION['sdtype'] = 5;
        $_SESSION['dtype'] = 5;
        exit('{"code":100,"msg":"用户注册成功"}');
    }
}

function getValiCode(){
    $valiCode = '';
    for($i=0;$i<4;$i++){
        $valiCode .= mt_rand(0,9);
    }
    $valiCode .=date('s');
    return $valiCode;
}
?>